package com.cnswhy.cloud.server.merchant.controller.shop;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.cnswhy.cloud.common.core.base.R;
import com.cnswhy.cloud.common.core.base.controller.SuperController;
import com.cnswhy.cloud.common.core.base.request.PageParams;
import com.cnswhy.cloud.common.core.entity.CurrentUser;
import com.cnswhy.cloud.common.core.mybatis.conditions.Wraps;
import com.cnswhy.cloud.common.core.mybatis.conditions.query.QueryWrap;
import com.cnswhy.cloud.common.core.utils.CnswhyUtil;
import com.cnswhy.cloud.common.core.utils.DateUtils;
import com.cnswhy.cloud.common.redis.service.IdGeneratorService;
import com.cnswhy.cloud.server.core.dto.ShopBrandPageDTO;
import com.cnswhy.cloud.server.core.dto.ShopBrandSaveDTO;
import com.cnswhy.cloud.server.core.dto.ShopBrandUpdateDTO;
import com.cnswhy.cloud.server.core.entity.ShopBrand;
import com.cnswhy.cloud.server.core.service.ShopBrandService;
import com.cnswhy.cloud.server.merchant.util.OssUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

//import com.github.zuihou.security.annotation.PreAuth;


/**
 * <p>
 * 前端控制器
 * 商品品牌
 * </p>
 *
 * @author cnswhy
 * @date 2020-10-10
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/shopBrand")
@Api(value = "ShopBrand", tags = "商品品牌")
//@PreAuth(replace = "shopBrand:")
public class ShopBrandController extends SuperController<ShopBrandService, Long, ShopBrand, ShopBrandPageDTO, ShopBrandSaveDTO, ShopBrandUpdateDTO> {
    @Autowired
    IdGeneratorService idGeneratorService;
    @ApiOperation(value = "同步图片")
    @PostMapping("/syncUrl")
    public R<Boolean> syncUrl() {
        List<ShopBrand> shopBrands = baseService.list();
        for(ShopBrand shopBrand : shopBrands){
            if(StringUtils.isBlank(shopBrand.getUrl())){

                shopBrand.setUrl(OssUtils.updateLoadFileToOss("shopBrand", "http://139.9.69.116:81/" + shopBrand.getBrandImg() ,idGeneratorService.genCommonIdString() + ".jpg"));
                baseService.updateById(shopBrand);
            }
        }
        return success();
    }

    @Override
    public void handlerQueryParams(PageParams<ShopBrandPageDTO> params) {
        /*CurrentUser currentUser = CnswhyUtil.getCurrentUser();
        log.info(JSON.toJSONString(currentUser));
        if(currentUser.getMerchantId() != null){
            params.getModel().setMerchantId(currentUser.getMerchantId());
        }*/
    }

    @Override
    public QueryWrap<ShopBrand> handlerWrapper(ShopBrand model, PageParams<ShopBrandPageDTO> params) {
        QueryWrap<ShopBrand> wrapper = model == null ? Wraps.q() : Wraps.q(model);

        if (CollUtil.isNotEmpty(params.getMap())) {
            Map<String, String> map = params.getMap();
            //拼装区间
            for (Map.Entry<String, String> field : map.entrySet()) {
                String key = field.getKey();
                String value = field.getValue();
                if (StrUtil.isEmpty(value)) {
                    continue;
                }
                if (key.endsWith("_st")) {
                    String beanField = StrUtil.subBefore(key, "_st", true);
                    wrapper.ge(getDbField(beanField, getEntityClass()), DateUtils.getStartTime(value));
                }
                if (key.endsWith("_ed")) {
                    String beanField = StrUtil.subBefore(key, "_ed", true);
                    wrapper.le(getDbField(beanField, getEntityClass()), DateUtils.getEndTime(value));
                }
            }
        }
        wrapper.orderByAsc("sort_order");
        return wrapper;
    }

    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list){
        List<ShopBrand> shopBrandList = list.stream().map((map) -> {
            ShopBrand shopBrand = ShopBrand.builder().build();
            //TODO 请在这里完成转换
            return shopBrand;
        }).collect(Collectors.toList());

        return R.success(baseService.saveBatch(shopBrandList));
    }
}
